#![feature(test)]

extern crate test;
use test::Bencher;

use rustpython_sre_engine::{Request, State, StrDrive};

struct Pattern {
    code: &'static [u32],
}

impl Pattern {
    fn state<'a, S: StrDrive>(&self, string: S) -> (Request<'a, S>, State) {
        self.state_range(string, 0..usize::MAX)
    }

    fn state_range<'a, S: StrDrive>(
        &self,
        string: S,
        range: std::ops::Range<usize>,
    ) -> (Request<'a, S>, State) {
        let req = Request::new(string, range.start, range.end, self.code, false);
        let state = State::default();
        (req, state)
    }
}

#[bench]
fn benchmarks(b: &mut Bencher) {
    // # test common prefix
    // pattern p1 = re.compile('Python|Perl') # , 'Perl'),    # Alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p1 = Pattern { code: &[14, 8, 1, 4, 6, 1, 1, 80, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 1] };
    // END GENERATED
    // pattern p2 = re.compile('(Python|Perl)') #, 'Perl'),  # Grouped alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p2 = Pattern { code: &[14, 8, 1, 4, 6, 1, 0, 80, 0, 17, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 17, 1, 1] };
    // END GENERATED
    // pattern p3 = re.compile('Python|Perl|Tcl') #, 'Perl'),        # Alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p3 = Pattern { code: &[14, 9, 4, 3, 6, 16, 80, 16, 84, 0, 7, 15, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 22, 11, 16, 80, 16, 101, 16, 114, 16, 108, 15, 11, 9, 16, 84, 16, 99, 16, 108, 15, 2, 0, 1] };
    // END GENERATED
    // pattern p4 = re.compile('(Python|Perl|Tcl)') #, 'Perl'),      # Grouped alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p4 = Pattern { code: &[14, 9, 4, 3, 6, 16, 80, 16, 84, 0, 17, 0, 7, 15, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 22, 11, 16, 80, 16, 101, 16, 114, 16, 108, 15, 11, 9, 16, 84, 16, 99, 16, 108, 15, 2, 0, 17, 1, 1] };
    // END GENERATED
    // pattern p5 = re.compile('(Python)\\1') #, 'PythonPython'),    # Backreference
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p5 = Pattern { code: &[14, 18, 1, 12, 12, 6, 0, 80, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 17, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 11, 0, 1] };
    // END GENERATED
    // pattern p6 = re.compile('([0a-z][a-z0-9]*,)+') #, 'a5,b7,c9,'), # Disable the fastmap optimization
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p6 = Pattern { code: &[14, 4, 0, 2, 4294967295, 23, 31, 1, 4294967295, 17, 0, 13, 7, 16, 48, 22, 97, 122, 0, 24, 13, 0, 4294967295, 13, 8, 22, 97, 122, 22, 48, 57, 0, 1, 16, 44, 17, 1, 18, 1] };
    // END GENERATED
    // pattern p7 = re.compile('([a-z][a-z0-9]*,)+') #, 'a5,b7,c9,'), # A few sets
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p7 = Pattern { code: &[14, 4, 0, 2, 4294967295, 23, 29, 1, 4294967295, 17, 0, 13, 5, 22, 97, 122, 0, 24, 13, 0, 4294967295, 13, 8, 22, 97, 122, 22, 48, 57, 0, 1, 16, 44, 17, 1, 18, 1] };
    // END GENERATED
    // pattern p8 = re.compile('Python') #, 'Python'),               # Simple text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p8 = Pattern { code: &[14, 18, 3, 6, 6, 6, 6, 80, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 1] };
    // END GENERATED
    // pattern p9 = re.compile('.*Python') #, 'Python'),             # Bad text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p9 = Pattern { code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 1] };
    // END GENERATED
    // pattern p10 = re.compile('.*Python.*') #, 'Python'),           # Worse text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p10 = Pattern { code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 24, 5, 0, 4294967295, 2, 1, 1] };
    // END GENERATED
    // pattern p11 = re.compile('.*(Python)') #, 'Python'),           # Bad text literal with grouping
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p11 = Pattern { code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 17, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 1] };
    // END GENERATED

    let tests = [
        (p1, "Perl"),
        (p2, "Perl"),
        (p3, "Perl"),
        (p4, "Perl"),
        (p5, "PythonPython"),
        (p6, "a5,b7,c9,"),
        (p7, "a5,b7,c9,"),
        (p8, "Python"),
        (p9, "Python"),
        (p10, "Python"),
        (p11, "Python"),
    ];

    b.iter(move || {
        for (p, s) in &tests {
            let (req, mut state) = p.state(s.clone());
            assert!(state.search(req));
            let (req, mut state) = p.state(s.clone());
            assert!(state.pymatch(&req));
            let (mut req, mut state) = p.state(s.clone());
            req.match_all = true;
            assert!(state.pymatch(&req));
            let s2 = format!("{}{}{}", " ".repeat(10000), s, " ".repeat(10000));
            let (req, mut state) = p.state_range(s2.as_str(), 0..usize::MAX);
            assert!(state.search(req));
            let (req, mut state) = p.state_range(s2.as_str(), 10000..usize::MAX);
            assert!(state.pymatch(&req));
            let (req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
            assert!(state.pymatch(&req));
            let (mut req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
            req.match_all = true;
            assert!(state.pymatch(&req));
        }
    })
}
